﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;

namespace Site_Extractor
{
	public class ForumTapuz
	{
		private static string
			   _mainLink = "http://www.tapuz.co.il/forums/forumsListNew.asp",
			   _linkPrefix = "http://www.tapuz.co.il/forums2008/forumpage.aspx?forumid=";

		public static List<string> GetAllForumLinks()
		{
			var res = ForumHandler.GatherAllLinks(_mainLink, isForumMine);
			return res;
		}
		private static bool isForumMine(string link)
		{
			if (!link.ToLower().StartsWith(_linkPrefix)) return false;
			return true;
		}
		public static List<string> GetAllMessageLinks(string forumLink)
		{
			forumLink += "&r=1";
			var page = HtmlDownloader.DownloadPage(new Uri(forumLink),Encoding.UTF8);
			return ForumHandler.GetFromString(page, "a(", ");");
		}
		public static ForumMessage GetMessage(string code)
		{
			ForumMessage msg = new ForumMessage();
			DateTime date;

			var args = ForumHandler.GetFromString(code, ",'", "',");
			GetUniqueId(code, out msg.UniqueId);
			ForumHandler.GetDate(args[0], "dd/MM/yy HH:mm", out date);
			msg.Topic = args[1];
			msg.Text = HtmlStripper.StripFromHtml(args[2]);
			msg.Date = date;

			return msg;
		}
		private static void GetUniqueId(string code, out long id)
		{
			int pos = code.IndexOf(",");
			if (pos < 0) { id = 0; return; }
			long.TryParse(code.Substring(0, pos), out id);
		}		
	}
}
